/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package basan.world;

import basan.components.RenderBoxComponent;
import basan.components.RenderPlaneComponent;
import basan.core.EntityManager;
import basan.core.GameObject;
import com.threed.jpct.*;
import java.util.logging.Level;

/**
 *
 * @author Menno @ Basans
 */
public class BasanWorld {
    public static World world;
    private FrameBuffer buffer;
    
    public BasanWorld () {
        world = new World();
        world.setAmbientLight(0, 255, 0);
        
        //Camera
        world.getCamera().setPosition(0, -1, 50);
        world.getCamera().lookAt(SimpleVector.create(0, 0, 0));
        
        //Renderbuffer
        buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
        buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
        buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
        
        //Some stuff should be cleaned.
        TextureManager.getInstance().addTexture("box", new Texture("resources/box.jpg"));
        TextureManager.getInstance().addTexture("ground", new Texture("resources/ground.png"));
        
        EntityManager em = EntityManager.getInstance();
        em.setWorld(world);
        
        //Testing game objects.
        GameObject block = em.createGameObject();
        block.addComponent(new RenderBoxComponent(block, "ground"));
        
        GameObject o = em.createGameObject(10f, 0f,0f);
        o.addComponent(new RenderBoxComponent(o, "box"));
        
        GameObject level =em.createGameObject();
        level.addComponent(new RenderPlaneComponent(level, "ground"));
 
        //loop de loop
        while( !org.lwjgl.opengl.Display.isCloseRequested()) {
            //box.rotateY((0.01f));
            
            em.updateAll();
            em.thinkAll();
            
            buffer.clear();
            world.renderScene(buffer);
            world.draw(buffer);
            buffer.update();
            buffer.displayGLOnly();
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                java.util.logging.Logger.getLogger(BasanWorld.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        //end of game/level/world/9-11 was an inside job
        buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
        buffer.dispose();
        System.exit(0);
    }
}
